<template>
  <div id="bg">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户登录日志</el-breadcrumb-item>
    </el-breadcrumb>
    <div id="tab">
      <!-- 搜索栏 -->
      <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="姓名">
            <el-input v-model="formInline.name" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="岗位">
            <el-input v-model="formInline.postname" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="登录时间">
            <el-date-picker v-model="value" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form>
      </div>
      <el-divider></el-divider>
      <!-- 新增按钮 -->
      <el-button type="primary" @click="addVisible=true">新增</el-button>
      <!-- 表格数据 -->
      <div id="table">
        <el-table :data="tablesData" stripe style="width: 100%">
          <el-table-column prop="l_id" label="序号" width="180">
          </el-table-column>
          <el-table-column prop="account" label="账号" width="180">
          </el-table-column>
          <el-table-column prop="password" label="密码" width="180">
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="post_name" label="岗位">
          </el-table-column>
          <el-table-column prop="l_login" label="登录时间">
          </el-table-column>
          <el-table-column prop="l_ip" label="登录IP">
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div id="pages">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!-- 新增弹框 formDialog-->
    <el-dialog title="新增登录日志" :visible.sync="addVisible" width="40%">
      <el-form :model="formDialog">
        <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="formDialog.order" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="工号" :label-width="formLabelWidth">
          <el-input v-model="formDialog.account" autocomplete="off" placeholder="200001"></el-input>
        </el-form-item>
        <el-form-item label="操作内容" :label-width="formLabelWidth">
          <el-input v-model="formDialog.text" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录时间" :label-width="formLabelWidth">
          <el-date-picker v-model="formDialog.value1" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" default-time="12:00:00" style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作时间" :label-width="formLabelWidth">
          <el-date-picker v-model="formDialog.value2" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" default-time="12:00:00" style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登录IP" :label-width="formLabelWidth">
          <el-input v-model="formDialog.ip" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      addVisible: false,
      formDialog: {
        order: '',
        account: '',
        text: '',
        value1: '',
        value2: '',
        ip: '',  //新增里面的时间
      },
      formInline: {
        name: '',
        postname: ''
      },
      value: '',  //搜索栏里面的时间
      tableData: [],
      page: 1,
      pageSize: 5,
      total: 0,
      formLabelWidth: '120px'
    }
  },
  methods: {
    onSearch() {
      this.page=1;
      this.$axios({
        url: '/system/systemlogin/get',
        params: {
          time: this.value,
          name: this.formInline.name,
          postname: this.formInline.postname
        }
      }).then((res) => {
        console.log(res);
        this.tableData = res.data.data
        this.total = res.data.total;
      })
    },
    onReset() {
      this.page = 1;
      this.getData();
      this.formInline.name = '';
      this.formInline.postname = '';
      this.value = '';
    },
    addOk() {
      console.log(this.formDialog)
      this.addVisible = false;
      this.$axios({
        url: '/system/systemlogin/add',
        method: 'post',
        data: {
          // l_id: this.formDialog.order,
          l_account: this.formDialog.account,
          l_text: this.formDialog.text,
          l_login: this.formDialog.value1,
          l_caozuo: this.formDialog.value2,
          l_ip: this.formDialog.ip
        }
      }).then((res) => {
        this.getData();
        this.formDialog = {
          order: '',
          account: '',
          text: '',
          value1: '',
          value2: '',
          ip: ''  //新增里面的时间
        }
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.onSearch();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
    },
    getData() {
      this.$axios({
        url: '/system/systemlogin/get',
        params: {
          time: '',
          // time: this.value,
          name: '',
          postname: ''
        }
      }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data
        this.total = res.data.total;
      })
    }
  },
  created: function () {
    this.getData();
  },
  computed: {
    tablesData() {
      return this.tableData.slice((this.page - 1) * this.pageSize, this.page * this.pageSize)
    }
  }
}
</script>
<style scoped>
.el-row span {
  cursor: pointer;
}
.el-main,
#bg {
  height: 100%;
}
.el-breadcrumb {
  margin: 20px;
}
#closeBtn {
  text-align: right;
}
#bg {
  background-color: rgb(242, 242, 242);
}
#tab {
  padding: 30px;
  background-color: #fff;
  margin: 0 20px;
}
.el-breadcrumb {
  margin-top: 0;
  padding-top: 20px;
}
#pages {
  margin-top: 20px;
  text-align: right;
}
</style>
